package com.along.study.Config;

import com.along.study.proxy.ServerSocket;
import com.along.study.proxy.VisitorSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

@Component
public class ServerListener implements ApplicationListener {

    @Autowired
    private ServerSocket serverSocket;

    @Autowired
    private VisitorSocket visitorSocket;

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if(event instanceof ApplicationReadyEvent){
            try {
                visitorSocket.startServer();
                serverSocket.startServer();
                System.out.println("服务已经启动了");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(event instanceof ContextClosedEvent){
            System.out.println("服务正在关闭了，bye");
        }
    }
}
